package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.dto.TagProducer;
import com.ttg.common.entities.BasePage;
import com.ttg.common.utils.DateTimeUtil;
import com.ttg.model.dto.MctDetailDto;
import com.ttg.model.dto.MctGrowAndIntegralSumDto;
import com.ttg.model.dto.VipDetailDto;
import com.ttg.model.mapper.VipGrowDetailMapper;
import com.ttg.model.pojo.VipGrowDetail;
import com.ttg.model.vo.VipBindAccountDetailVO;
import com.ttg.web.dao.VipGrowDetailDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

@Repository
public class VipGrowDetailDaoImpl extends ServiceImpl<VipGrowDetailMapper, VipGrowDetail> implements VipGrowDetailDao {
    @Override
    public BasePage<VipGrowDetail> getVipGrowDetail(BasePage<VipGrowDetail> page,VipDetailDto vipDetailDto) {
        LambdaQueryWrapper<VipGrowDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VipGrowDetail::getVipId,vipDetailDto.getVipId())
                .eq(StringUtils.isNotBlank(vipDetailDto.getTaskName()),VipGrowDetail::getTaskName,vipDetailDto.getTaskName())
                .eq(StringUtils.isNotBlank(vipDetailDto.getTaskId()),VipGrowDetail::getTaskId,vipDetailDto.getTaskId())
                .eq(StringUtils.isNotBlank(vipDetailDto.getId()),VipGrowDetail::getId,vipDetailDto.getId());
        return page(page,queryWrapper);
    }

    @Override
    public BasePage<VipGrowDetail> getMctGrowDetail(BasePage<VipGrowDetail> page, MctDetailDto mctDetailDto) {
        LambdaQueryWrapper<VipGrowDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VipGrowDetail::getMctNo,mctDetailDto.getMctNo())
                .eq(StringUtils.isNotBlank(mctDetailDto.getTaskName()),VipGrowDetail::getTaskName,mctDetailDto.getTaskName())
                .eq(StringUtils.isNotBlank(mctDetailDto.getTaskId()),VipGrowDetail::getTaskId,mctDetailDto.getTaskId())
                .eq(StringUtils.isNotBlank(mctDetailDto.getId()),VipGrowDetail::getId,mctDetailDto.getId());
        return page(page,queryWrapper);
    }

    @Override
    public List<VipBindAccountDetailVO> getVipBindAccountDetail(VipDetailDto vipDetailDto) {
        List<VipBindAccountDetailVO> vipBindAccountDetail = baseMapper.selectVipBindAccountDetail(vipDetailDto);
        return vipBindAccountDetail;
    }

    @Override
    public List<VipGrowDetail> getVipDetailByVipIdAndTime(Long vipId,Long mctNo,Long taskId, LocalDate starTime, LocalDate endTime) {
        LambdaQueryWrapper<VipGrowDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(vipId) ,VipGrowDetail::getVipId,vipId);
        queryWrapper.eq(Objects.nonNull(mctNo) ,VipGrowDetail::getMctNo,mctNo);
        queryWrapper.between(VipGrowDetail::getUpdateTime,starTime,endTime);
        queryWrapper.eq(VipGrowDetail::getTaskId,taskId);
        return list(queryWrapper);
    }

    @Override
    public BasePage<VipBindAccountDetailVO> getVipBindAccountDetailPage(BasePage<VipBindAccountDetailVO> basePage, VipDetailDto vipDetailDto) {
        return  baseMapper.selectVipBindAccountDetailPage(basePage,vipDetailDto);
    }

    @Override
    public BasePage<VipGrowDetail> getVipIntegralDetail(BasePage<VipGrowDetail> page, VipDetailDto vipDetailDto) {
        LambdaQueryWrapper<VipGrowDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VipGrowDetail::getVipId,vipDetailDto.getVipId())
                .eq(StringUtils.isNotBlank(vipDetailDto.getTaskName()),VipGrowDetail::getTaskName,vipDetailDto.getTaskName())
                .eq(StringUtils.isNotBlank(vipDetailDto.getTaskId()),VipGrowDetail::getTaskId,vipDetailDto.getTaskId())
                .eq(StringUtils.isNotBlank(vipDetailDto.getId()),VipGrowDetail::getId,vipDetailDto.getId());
        return page(page,queryWrapper) ;
    }

    @Override
    public BasePage<VipGrowDetail> getMctIntegralDetail(BasePage<VipGrowDetail> page, MctDetailDto mctDetailDto) {
        LambdaQueryWrapper<VipGrowDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VipGrowDetail::getMctNo,mctDetailDto.getMctNo())
                .eq(StringUtils.isNotBlank(mctDetailDto.getTaskName()),VipGrowDetail::getTaskName,mctDetailDto.getTaskName())
                .eq(StringUtils.isNotBlank(mctDetailDto.getTaskId()),VipGrowDetail::getTaskId,mctDetailDto.getTaskId())
                .eq(StringUtils.isNotBlank(mctDetailDto.getId()),VipGrowDetail::getId,mctDetailDto.getId());
        return page(page,queryWrapper);
    }

    @Override
    public List<VipGrowDetail> getVipIntegralDetailByVipId(Long vipId) {
        LambdaQueryWrapper<VipGrowDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(VipGrowDetail::getVipId,vipId)
                .between(VipGrowDetail::getUpdateTime, DateTimeUtil.getToday000(),DateTimeUtil.getToday235959());
        return list(wrapper);
    }

    @Override
    public int sumMctGrowAndIntegral(MctGrowAndIntegralSumDto reqDto, TagProducer tagProducer) {
        return baseMapper.sumMctGrowAndIntegral(reqDto,tagProducer);
    }

    @Override
    public int sumVipGrowAndIntegral(MctGrowAndIntegralSumDto reqDto, TagProducer tagProducer) {
        return baseMapper.sumVipGrowAndIntegral(reqDto,tagProducer);
    }

    @Override
    public void deleteGrowDetailByDate(Integer localDate) {
        LambdaUpdateWrapper<VipGrowDetail> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(VipGrowDetail::getDate,localDate);
        baseMapper.delete(wrapper);
    }

    @Override
    public void removeByTaskIdAndDate(Long taskId, Integer nowDate) {
        LambdaUpdateWrapper<VipGrowDetail> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(VipGrowDetail::getTaskId,taskId)
                .eq(VipGrowDetail::getDate,nowDate);
        remove(wrapper);
    }

}
